'''
https://leetcode.cn/problems/make-sum-divisible-by-p/description/
'''
from typing import List


class Solution:
    def minSubarray(self, nums: List[int], p: int) -> int:
        if mod := (sum(nums) % p) == 0: return 0
        dic = {0: -1}
        cur = 0
        ans = len(nums)
        for i, num in enumerate(nums):
            cur = (cur + num) % p
            find = (cur + p - mod) % p
            if find in dic:
                ans = min(ans, i - dic[find])
            dic[cur] = i
        return -1 if ans == len(nums) else ans
